//
//  DrawerController.m
//  第8组,牛逼哄哄
//
//  Created by  陈聪 on 16/5/4.
//  Copyright © 2016年 cc. All rights reserved.
//

#import "DrawerController.h"

@interface DrawerController ()
#define maxX (0.8 * MMScreenW)
#define centerX (0.5 * MMScreenW)
#define leftControllerOriginX -180

@end

@implementation DrawerController
- (instancetype)initWithContentViewController:(UIViewController *)contentViewController leftMenuViewController:(UIViewController *)leftMenuViewController{
    if (self = [super init]) {
        self.contentViewController = contentViewController;
        contentViewController.view.frame = self.view.bounds;
        self.mainView = contentViewController.view;
        
        self.leftMenuViewController = leftMenuViewController;
        leftMenuViewController.view.frame = CGRectMake(leftControllerOriginX, 0, MMScreenW, MMScreenH);
        self.leftView = leftMenuViewController.view;
        [self.view addSubview:self.leftView];
        [self.view addSubview:self.mainView];
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self addGesture];
    
}
- (void)setMyContentViewController:(UIViewController *)contentViewController{
    self.contentViewController = contentViewController;
    contentViewController.view.frame = CGRectMake(180, 0, MMScreenW, MMScreenH);
//    [self.mainView removeFromSuperview];
    self.mainView  = contentViewController.view;
    [self.view addSubview:self.mainView];
    MMLog(@"%@",NSStringFromCGRect(self.mainView.frame));
    MMLog(@"%@",NSStringFromCGRect(contentViewController.view.frame));
}

#pragma mark - Gesture
- (void)addGesture{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.mainView addGestureRecognizer:tap];
}

- (void)tap:(UIPanGestureRecognizer *)tap{
    if (self.mainView.x == 0) {
        tap.cancelsTouchesInView = NO;
    }
    [self closeDrawerWithAnimateDuration:0.3  ] ;
}

- (void)pan:(UIPanGestureRecognizer *)pan{
    CGFloat offsetX = [pan translationInView:self.mainView].x;
    [self setTransform:offsetX];
    //复位
    [pan setTranslation:CGPointZero inView:self.mainView];
    CGFloat target = 0.0;
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (self.mainView.frame.origin.x > -leftControllerOriginX * 0.5) {
            target =  -leftControllerOriginX ;
        }
        offsetX = target - self.mainView.frame.origin.x;
        [UIView animateWithDuration:0.3 animations:^{
            [self setTransform:offsetX];
        }];
    }
}

- (void)setTransform:(CGFloat)offsetX{
    CGFloat tempX = self.mainView.frame.origin.x;
    tempX +=  offsetX;
    if (tempX < 0.0 || tempX > -leftControllerOriginX) {
        
    }else{
        CGRect temp = self.mainView.frame;
        temp.origin.x = tempX;
        self.mainView.frame = temp;
        CGRect temp1 = self.leftView.frame;
        temp1.origin.x += offsetX;
        self.leftView.frame = temp1;
    }
}

#pragma mark - 关闭抽屉
- (void)closeDrawerWithAnimateDuration:(NSTimeInterval)duration
{
    if (!duration)  duration = 0.3;
    [UIView animateWithDuration:duration animations:^{
        [self setTransform:leftControllerOriginX ];
    } completion:nil];
}
#pragma mark - 打开抽屉
- (void)openDrawerWithAnimateDuration:(NSTimeInterval)duration
{
    if (!duration)  duration = 0.3;
    [UIView animateWithDuration:duration animations:^{
        [self setTransform:-leftControllerOriginX ];
    } completion:nil];
    
    
}
@end
